package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.execption.XueChengPlusException;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.model.dto.AddCourseTeacherDto;
import com.xuecheng.content.model.dto.EditCourseTeacherDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.service.CourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description 课程老师接口
 * @Author dingchunlin
 * @Date 2024/3/30 11:23
 * @Version 1.0.0
 */
@Slf4j
@Service
public class CourseTeacherServiceImpl implements CourseTeacherService {

    @Autowired
    CourseTeacherMapper courseTeacherMapper;

    @Autowired
    CourseBaseMapper courseBaseMapper;



    /**
     * 根据课程id查询教师信息
     * @param courseId
     * @return
     */
    @Override
    public List<CourseTeacher> selectByCourseId(Long courseId) {
        LambdaQueryWrapper<CourseTeacher> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CourseTeacher::getCourseId,courseId);
        List<CourseTeacher> courseTeacherList = courseTeacherMapper.selectList(lambdaQueryWrapper);
        return courseTeacherList;
    }

    /**
     * 添加课程教师信息
     * @param courseTeacherDto
     * @param companyId
     * @return
     */
    @Override
    public CourseTeacher saveOrEditCourseTeacher(Long companyId , EditCourseTeacherDto courseTeacherDto) {
        //首先获取课程id
        Long courseId = courseTeacherDto.getCourseId();
        if (courseId == null){
            throw new XueChengPlusException("请输入课程id,否则不能新增");
        }
        //查询课程的基本信息
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        if (courseBase == null){
            throw new XueChengPlusException("该门课程不存在,请重试");
        }
        //判断该门课程是否是同一机构
        if (!companyId.equals(courseBase.getCompanyId())){
            throw new XueChengPlusException("本机构只能修改本机构的课程");
        }
        //判断新增的老师是否已经存在
        String teacherName = courseTeacherDto.getTeacherName();
        CourseTeacher courseTeacherDb = courseTeacherMapper.selectByCourseIdAndTeacherName(courseId, teacherName);
        if (courseTeacherDb != null){
            throw new XueChengPlusException("该老师已经添加，请勿重新添加");
        }
        CourseTeacher courseTeacher = new CourseTeacher();
        //判断是新增还是修改
        Long id = courseTeacherDto.getId();
        //进行新增
        if (id == null){
            BeanUtils.copyProperties(courseTeacherDto, courseTeacher);
            courseTeacher.setCreateDate(LocalDateTime.now());
            courseTeacherMapper.insert(courseTeacher);
            return courseTeacher;
        }else {
            //进行修改
            CourseTeacher dbCourseTeacher = courseTeacherMapper.selectById(id);
            BeanUtils.copyProperties(courseTeacherDto, dbCourseTeacher);
            courseTeacherMapper.updateById(dbCourseTeacher);
            return dbCourseTeacher;
        }
    }

    /**
     * 删除课程教师
     * @param companyId
     * @param courseId
     * @param courseTeacherId
     */
    @Override
    public void deleteCourseTeacher(Long companyId, Long courseId, Long courseTeacherId) {
        //查询课程的基本信息
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        if (courseBase == null){
            throw new XueChengPlusException("该门课程不存在,请重试");
        }
        //判断该门课程是否是同一机构
        if (!companyId.equals(courseBase.getCompanyId())){
            throw new XueChengPlusException("本机构只能删除本机构的老师");
        }
        //进行删除
        courseTeacherMapper.deleteById(courseTeacherId);
    }
}
